<?php
/**
 * Created by PhpStorm.
 * User: 张福来
 * Date: 2018/10/27
 * Time: 11:38
 */
namespace app\api\controller;
use app\api\model\Carinfor as carinfor;
use app\api\model\Device as DeviceModel;
use app\api\validate\Apivalidate;
use app\obd\model\CarModel;
use think\cache\driver\Redis;
use think\Db;
use think\Request;

/*
* Notes:车辆相关模块
* Create Date: 2018/10/27
* Create Time: 11:35
* Create by :张福来
* Param :
*/
class Car extends Base{
    /*
    * Notes: 查询车辆信息
    * Create Date: 2018/10/3
    * Create Time: 上午9:25
    * Create by : 张福来
    * param :
    * $b_id : 车辆品牌id
    * $t_id : 车辆系别id
    * $m_id : 车辆车型id
    * $pageNum : 分页 页数
    * $listNum : 查询条数 默认10条
    * $start_time : 开始时间
    * $end_time : 结束时间
    */
    public function selectCarinfo(Request $request){
        $car  = new carinfor();
        $postData = $request ->post();
        $check = $this->validate($postData,"CarValidate");
        if($check !== true){
            apiResponse(201,$check,"");
        }
        $pageNum = $postData["pagenum"];
        $listNum = $postData["listNum"];
        $w["status"] = array('NEQ',9);
        if(!empty($listNum)) $listNum = 10;
        if(!empty($postData["b_id"])) $w["b_id"] = $postData["b_id"];
        if(!empty($postData["u_id"])) $w["u_id"] = $postData["u_id"];
        if(!empty($postData["t_id"])) $w["t_id"] = $postData["t_id"];
        if(!empty($postData["m_id"])) $w["m_id"] = $postData["m_id"];
        if(!empty($postData["a_id"])) $w["a_id"] = $postData["a_id"];
        if(!empty($postData["c_id"])){
            $w['c_id'] = $postData["c_id"];
        }else{
            $w['c_id'] = array('NEQ',0);
        }
        if(!empty($postData["start_time"]) && !empty($postData["end_time"])){
            $start_time = $postData["start_time"]. "00:00:00";
            $end_time = $postData["end_time"]. " 23:59:59";
            $w['ctime'] = array(array('EGT',strtotime($start_time)),array('ELT',strtotime($end_time)),'and');
        }
        $result = $car->selectCarinfo($pageNum,$listNum,$w);
        if(empty($result['list'])){
            return apiResponse("200","数据为空",$result);
        }else{
            return apiResponse("200","查询成功",$result);
        }
    }

    /**
     *  添加用户车辆  代码重构
     * @param Request $request
     * @return mixed
     * @author zhenHong~
     */
    public function addCar(Request $request)
    {
        $postData = $request->post();
        $check = $this->validate($postData,"CarValidate.addCar");
        if($check !== true){
            return apiResponse(201,$check,"");
        }
        $car  = new carinfor();
        //清除其他默认车辆
        $car->noDefaultCar($postData['u_id']);
        $is_add = $car->addCarInfo($postData);
        if ($is_add) {
            return apiResponse(200,"添加成功", '');
        }else{
            return apiResponse(201,"添加失败", '');
        }
    }
    /**
     * 用户查询车辆列表信息
     * @author 王涛
     * @DataTime 2018-08-09T10:24:16+0800
     * @return   [type]                   [description]
     */
    public function carsFind(Request $request)
    {
        $postData = $request->post();
        $result = $this->validate($postData,'CarValidate.carsFind');
        if($result !== true){
            return apiResponse(201,$result,'');
        }
        $carinfo = new carinfor();
        $where['u_id']   = $postData['u_id'];
        $where['status'] = array("NEQ",9);
        $carData = $carinfo->carsInfoByUser($where);
        if ($carData) {
            return apiResponse(200,"成功", $carData);
        }else{
            return apiResponse(202,"没有车辆", '');
        }
    }
    /**
     * 用户查询车辆详情
     * @author 王涛
     * @DataTime 2018-08-11T15:47:25+0800
     * @return   [type]                   [description]
     */
    public function carInfo(Request $request)
    {
        $postData = $request->post();
        $result = $this->validate($postData,'CarValidate.carInfo');
        if($result !== true){
            return apiResponse(201,$result,'');
        }
        $carinfo = new carinfor();
        $carData = $carinfo->carInfoByUser(['c_id' => $postData['c_id']]);
        if($carData){
            return apiResponse(200,"成功", $carData);
        }else{
            return apiResponse(201,'暂无数据','');
        }
    }
    /**
     * 用户编辑车辆
     * @author 王涛
     * @DataTime 2018-08-11T15:55:28+0800
     * @return   [type]                   [description]
     */
    public function carEdit(Request $request)
    {
        $postData = $request->post();
        $result = $this->validate($postData,'CarValidate.carEdit');
        if($result !== true){
            return apiResponse(201,$result,'');
        }
        $carM = new carinfor();
        $is_edit = $carM->editCarInfo(['c_id' => $postData['c_id'] ],$postData);
        if ($is_edit) {
            return apiResponse(200,"编辑成功", '');
        }else{
            return apiResponse(201,"编辑失败", '');
        }
    }


    /**
     * 用户删除车辆
     * @author topqiang
     * @DataTime 2018-08-11T15:55:28+0800
     * @return   [type]                   [description]
     */
    public function carDel(Request $request)
    {
        $postData = $request->post();
        $result = $this->validate($postData,'CarValidate.carDel');
        if($result !== true){
            return apiResponse(201,$result,'');
        }
        $carM = new carinfor();
        $is_edit = $carM->editCarInfo(['c_id' => $postData['c_id']],['status' => 9]);
        if ($is_edit) {
            return apiResponse(200,"删除成功", '');
        }else{
            return apiResponse(201,"删除失败", '');
        }
    }


    /**
     * 设置默认车辆
     * @author 王涛
     * @DataTime 2018-08-09T16:24:16+0800
     * @return   [type]                   [description]
     */
    public function carDefault(Request $request)
    {
        $postData = $request->post();
        $result = $this->validate($postData,'CarValidate.carDefault');
        if($result !== true){
            return apiResponse(201,$result,'');
        }
        $car = new carinfor;
        //清除其他默认车辆
        $car->noDefaultCar($postData['u_id']);
        //设置默认车辆
        $is_default = $car->defaultCar($postData);
        if ($is_default) {
            return apiResponse(200,"设置成功", '');
        }else{
            return apiResponse(201,"设置失败", '');
        }
    }

}